package leetcode.weekly.week308;

//Solution3Test
public class Solution3 {

	public int garbageCollection(String[] garbage, int[] travel) {
		int total = 0;
		int len = garbage.length;
		int[] sums = new int[len];
		for (int i = 1; i < len; i++) {
			sums[i] = sums[i - 1] + travel[i - 1];
		}
		for (int i = 0, m = 0, p = 0, g = 0; i < len; i++) {
			String cur = garbage[i];
			int cm = 0, cp = 0, cg = 0;
			for (char c : cur.toCharArray()) {
				if (c == 'M') {
					cm++;
				} else if (c == 'P') {
					cp++;
				} else if (c == 'G') {
					cg++;
				}
			}
			if (cm > 0) {
				total += cm;
				total += sums[i] - sums[m];
				m = i;
			}
			if (cp > 0) {
				total += cp;
				total += sums[i] - sums[p];
				p = i;
			}
			if (cg > 0) {
				total += cg;
				total += sums[i] - sums[g];
				g = i;
			}
		}
		return total;
	}

}
